//
//  LibsManager.swift
//  FrameProduct
//
//  Created by delegate on 2024/2/3.
//

import Foundation
import IQKeyboardManagerSwift
import YYText
import Bugly
#if DEBUG
import FLEX
#endif

/// 这是用于配置app中使用的所有第三方库的管理器类
struct LibsManager {
    
    static let shared = LibsManager()
    
    func setupLibs() {
        setupKeyboardManager()
        setupBugly()
        setupFLEX()
        setupAppearance()
    }
    func setupKeyboardManager() {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
            IQKeyboardManager.shared.registerTextFieldViewClass(YYTextView.self, didBeginEditingNotificationName:NSNotification.Name.YYTextViewTextDidBeginEditing.rawValue, didEndEditingNotificationName: NSNotification.Name.YYTextViewTextDidEndEditing.rawValue)
            IQKeyboardManager.shared.enable = true
            IQKeyboardManager.shared.enableAutoToolbar = true
            IQKeyboardManager.shared.shouldResignOnTouchOutside = true // 控制点击背景是否收起键盘
        }
    }
    private func setupBugly() {
        Bugly.start(withAppId: Configs.Keys.bugly.appId)
    }
    
    private func setupFLEX() {
        #if DEBUG
        FLEXManager.shared.isNetworkDebuggingEnabled = true
        #endif
    }
    private func setupAppearance() {
        if #available(iOS 15.0, *) {
            UITableView.appearance().sectionHeaderTopPadding = 0
            UITableView.appearance().fillerRowHeight = 0
        }
        // 统一修改textField的光标颜色
        UITextField.appearance().tintColor = UIColor.lxs.mainColor
        // 统一修改UISearchBar的取消按钮字体和颜色
        let attributes = [
            NSAttributedString.Key.foregroundColor : hexColor("#FFFFFF"),
            NSAttributedString.Key.font : UIFont.lxs.font(size: 16)
        ]
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
//        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .selected)
    }
    /*
    private func configNetworking() {
        
        #if DEBUG
        NetworkingConfig.shared.isEnableLogging = true
        #else
        NetworkingConfig.shared.isEnableLogging = false
        #endif
        NetworkingConfig.shared
            .headers {
                var headers = Configs.BaseApi.headers
                if let role = UserManager.shared.role {
                    headers["ROLE"] = "\(role.type)"
                }
                return headers
            }
            .successCodes {
                return [200, 201, 100, 105, 401] // 100, 105, 401 这三个code为强制退出登录的code，不处理
            }
            .ignoreCodes {
                return [401] // 401 这个code为强制退出登录的code，不处理
            }
            .plugins {
                return [AuthCheckPlugin.default]
            }
    }
     */
}
